package com.tec.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tec.security.entity.NewsLog;
import com.tec.security.service.NewsLogService;
import com.tec.security.mapper.NewsLogMapper;
import com.tec.security.utils.date.DateString;
import com.tec.security.utils.other.LimitPage;
import com.tec.security.utils.security.entity.UserInformation;
import com.tec.security.utils.security.entity.UserKey;
import com.tec.security.utils.security.mapper.UserKeyMapper;
import com.tec.security.utils.security.service.UserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author LL318
* @description 针对表【news_log】的数据库操作Service实现
* @createDate 2023-12-01 01:11:27
*/
@Service
public class NewsLogServiceImpl extends ServiceImpl<NewsLogMapper, NewsLog>
    implements NewsLogService{
    private final UserInformationService userInformationService;
    private final UserKeyMapper userKeyMapper;
    private final NewsLogMapper newsLogMapper;
    @Autowired
    public NewsLogServiceImpl(UserInformationService userInformationService, UserKeyMapper userKeyMapper, NewsLogMapper newsLogMapper) {
        this.userInformationService = userInformationService;
        this.userKeyMapper = userKeyMapper;
        this.newsLogMapper = newsLogMapper;
    }

    @Override
    public void newsInfo(String loginID, String message, String type) {
        UserInformation userInformation=userInformationService.selectInformationByUUID(loginID);
        QueryWrapper<UserKey> userKeyQueryWrapper=new QueryWrapper<>();
        userKeyQueryWrapper.select("username").eq("uuid",loginID);
        NewsLog newsLog=new NewsLog();
        newsLog.setName(userInformation.getName());
        newsLog.setMessage(message);
        newsLog.setUsername(userKeyMapper.selectOne(userKeyQueryWrapper).getUsername());
        newsLog.setDate(DateString.currentDate());
        newsLog.setTime(DateString.currentTime());
        newsLog.setType(type);
        newsLogMapper.insert(newsLog);
    }

    @Override
    public Map<String, Object> allNewsLogList(LimitPage limitPage, String type) {
        QueryWrapper<NewsLog> newsLogQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(limitPage.getPage(), limitPage.getSize());
        Map<String,Object> result=new HashMap<>();
        QueryWrapper<NewsLog> newsLogCountQueryWrapper=new QueryWrapper<>();
        if(!Objects.equals(type, "全部")){
            newsLogQueryWrapper.eq("message",type);
            newsLogCountQueryWrapper.eq("message",type);
        }
        result.put("list",newsLogMapper.selectList(newsLogQueryWrapper));
        result.put("total",newsLogMapper.selectCount(newsLogCountQueryWrapper));
        return result;
    }
}




